####TODO: ACTUAL DAMN PASSWORD STUFF... but this is a mud? idk
#class contains all data player would need
import pymysql
import configurethis
import time
from collections import deque
cfgstuff = configurethis.Configit()

class mobile:
    stuff = cfgstuff.dbpas
    stuffu = cfgstuff.dbuser
    stuffp = cfgstuff.dbport
    stuffhost = cfgstuff.dbhost
    sqldebug = True
    
    mobindex = []
    
    try:
        connsql = pymysql.connect(host= stuffhost, port=stuffp, user=stuffu, passwd= stuff, db='muddymoon', )
        connsqldict = pymysql.connect(host=stuffhost, port=stuffp, user=stuffu, passwd= stuff, db='muddymoon', cursorclass=pymysql.cursors.DictCursor)
    
        cursor = connsql.cursor()
        cursord = connsqldict.cursor()
    except pymysql.err.OperationalError:
        print("ERROR ERROR ERROR")
        print("Access denied to DB or some other DB related error, fix user name or password to the db. maybe port?")
        quit()
    #player data/stats
    #two names(other than uname). character name and a fancy/infamous name
    hp_base = 10
    HP = 10
    skill = 1
    power = 3
    skill = 0
    
    connection = ''
    def __init__(self):

        pass
          
            
       
    def user_exists(self,inpname):
        self.cursor.execute("SELECT count(*) FROM player WHERE user_name =%s ", (inpname,))
        self.connsql.commit()
        tester = self.cursor.fetchone()
        
        if tester[0] == 0:
            return False
        if tester[0] > 0:
            return True
    def addmobile(self, username, desc, char):
        #something something take in user name and password, generate userID?

        
        
        queery = "INSERT INTO mobile(user_name, desc, character_a, map, map_loc, p_power, p_skill, p_health, p_base_health, p_modifier) VALUES(%s, %s, %s,0,0,1,1,1,1,1)"
        if self.sqldebug:
            print(queery)

        self.cursor.execute(queery, (username, desc, char))
        self.connsql.commit()
        #register the mobile into the master list of mobiles
        
    def loginproc(self, uname, passinput):
        
        self.cursord.execute("SELECT pword from player WHERE user_name = %s", (uname,))
        self.connsqldict.commit()
        loginpasser = self.cursord.fetchone()
        loginpasserun = loginpasser['pword']
        if passinput == loginpasserun:
            return True
        else:
            return False

        
    def lastconnsav(self, dict):
        #writes last connection and time
        #in db, last_time was the last connection time, last conn was just in case I could implement something else.
        #TODO: possibly save entire pdict?
        #conntime = "UPDATE player SET last_connection=%s WHERE user_name=%s"
        
        #self.cursor.execute(conntime, (time.strftime('%Y-%m-%d %H:%M:%S'), uname))

        #TODO:  Skill, power can be updated as a bonus thing later.
        # add a thing for mobiles to have an actions list by ID
        if dict['id']:
            sqlsez = "UPDATE player SET last_connection=%s, character_a=%s, character_b=%s, map=%s, map_loc=%s, p_health=%s, p_base_health=%s, p_modifier=%s WHERE id=%s"
            self.cursor.execute(sqlsez, (time.strftime('%Y-%m-%d %H:%M:%S'), dict['character_a'], dict['character_b'], int(dict['map']), int(dict['map_loc']), int(dict['p_health']), int(dict['p_base_health']), int(dict['p_modifier']), int(dict['id']), ))
            self.connsql.commit()
        else:
            pass
    
    def getdatadict(self, uname):
        ##### a sketch of what this might look like
        sqlsez = "SELECT id, user_name, last_connection, last_time, character_a, character_b, map, map_loc,  creation_time, p_power, p_skill, p_health, p_base_health, p_modifier FROM player WHERE user_name=%s"
        self.cursord.execute(sqlsez, (uname,))
        self.connsqldict.commit()
        dataz = self.cursord.fetchone()
        return dataz
    def wrangle_mobiles(self):
        #loads all the mobiles somehow, and does any initial cleanup
        pass
    def mobile_all_check(self, queue):
        #takes in the global check to check for messages
        #somehow turns this into things does
        pass

    def load_test_mobile(self, name):
        
        #self.mobindex.append({'id':0,'map':0,'map_loc':0 'character_a':name })
        testm = {'id':0,'map':0,'map_loc':0, 'character_a':name, 'event':4444}
        return testm
        #return self.mobindex
